package com.leetcode.algorithm.y22.m09.w3;

/**
 * 50. Pow(x, n)
 * 
 * https://leetcode.cn/problems/powx-n/
 * 
 * @author jie.deng
 *
 */
class Question0050Solution01 {

	public double myPow(double x, int n) {
		if (x == 0) {
			return 0;
		}
		if (x == 1 || n == 0) {
			return 1;
		}
		if (x == -1) {
			return (n & 1) == 1 ? -1 : 1;
		}
		double ans = 1.0;
		if (n < 0) {
			x = 1.0 / x;
			if (n == Integer.MIN_VALUE) {
				n = Integer.MIN_VALUE + 1;
				ans = x;
			}
			n = -n;
		}
		double y = myPow(x, n / 2);
		if ((n & 1) == 1) {
			return ans * y * y * x;
		}
		return ans * y * y;
	}

}